package com.dkha.server.controllers;

import com.dkha.common.enums.ApiUrlEnum;
import com.dkha.common.exception.DkException;
import com.dkha.common.http.HttpUtil;
import com.dkha.common.modules.vo.CompareMuch;
import com.dkha.common.modules.vo.ReturnVO;
import com.dkha.common.modules.vo.face.ApiFaceOneSearchReturnFaceVO;
import com.dkha.common.modules.vo.facelib.CompareRequestVo;
import com.dkha.common.modules.vo.facelib.OneToOneResultVo;
import com.dkha.common.modules.vo.search.FaceCompareVO;
import com.dkha.common.result.CommonResult;
import com.dkha.common.util.JsonUtil;
import com.dkha.common.util.UtilValidate;
import com.dkha.server.services.ICompareService;
import com.fasterxml.jackson.core.type.TypeReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Spring
 * @Since 2019/12/11 17:05
 * @Description
 */
@RestController
@RequestMapping(value = "/compare")
@Api(tags = "比对相关")
@Slf4j
public class CompareController extends CommonResult {

    @Autowired
    private ICompareService compareService;
    @Value("${api.server.prefix}")
    private String apiServerPrefix;
    @Autowired
    private HttpUtil httpUtil;

    @PostMapping(value = "/grouping")
    @ApiOperation(value = "分组比对")
    @RequiresPermissions("face:group:comparison")
    public CommonResult groupingCompare(
            @ApiParam(required = true, name = "compareRequestVo", value = "")
            @RequestBody CompareRequestVo compareRequestVo) {
        return successResult(compareService.groupingCompare(compareRequestVo));
    }

    @PostMapping(value = "/oneToOne")
    @ApiOperation(value = "一比一比对")
    @RequiresPermissions("face:onetoone:comparison")
    public CommonResult oneToOneCompare(
            @ApiParam(required = true, name = "compareRequestVo", value = "")
            @RequestBody CompareRequestVo compareRequestVo) {
        return successResult(compareService.oneToOne(compareRequestVo));
    }
    @PostMapping(value = "/oneToMuch")
    @ApiOperation(value = "一比多比对")
//    @RequiresPermissions("face:onetoone:comparison")
    public CommonResult oneToMuchCompare(
            @ApiParam(required = true, name = "compareRequestVo", value = "")
            @RequestBody CompareRequestVo compareRequestVo) {
        System.out.println("comparerequest"+compareRequestVo);
        String apiUrl = apiServerPrefix + ApiUrlEnum.FACE_COMPARISON_MUCH.getUrl();
        // String apiUrltest = "http://192.168.1.196:5001/compare";
        FaceCompareVO faceCompareVO = new FaceCompareVO();
        faceCompareVO.setImage1(compareRequestVo.getUrlList().get(0));
        faceCompareVO.setExtraMeta(compareRequestVo.getValue());
        ReturnVO returnVO = (ReturnVO)httpUtil.post(apiUrl, faceCompareVO, ReturnVO.class);
        CompareMuch compareMuch = JsonUtil.string2Obj(JsonUtil.obj2String(returnVO.getData()), new TypeReference<CompareMuch>() {});
        //返回结果集
        OneToOneResultVo oneToOneResultVo = new OneToOneResultVo();
        if (UtilValidate.isNotEmpty(compareMuch)) {
            oneToOneResultVo.getUrls().add(compareMuch.getRphoto());
            oneToOneResultVo.setScore(compareMuch.getHitSimilarity());
        }
        System.out.println(oneToOneResultVo);
        return successResult(oneToOneResultVo);
    }
    @PostMapping(value = "/oneToOneImage")
    @ApiOperation(value = "一比一比对上传 Image")
    //@RequiresPermissions("employment:contract:upload")
    public CommonResult oneToOneCompareImage1(
            @ApiParam(required = true, name = "compareRequestVo", value = "")
            @RequestBody CompareRequestVo compareRequestVo) {
        if (UtilValidate.isEmpty(compareRequestVo.getUrlList())) {
            throw new DkException("请选择需要比对的图片");
        }
        String fileName =  compareRequestVo.getUrlList().get(0);
        return successResult(compareService.setFaceCompare(fileName.substring(fileName.indexOf(",")+1)));
    }
}
